//
//  XJIWantServiceFooterView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/16.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  我要服务的footerView

#import "XJIWantServiceFooterView.h"

//cell
#import "XJIWantServiceFooterItem.h"

@interface XJIWantServiceFooterView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UITextField *descTF; //描述
@property (nonatomic, strong) UICollectionView *clv; //选择图片的collectionView

@end

@implementation XJIWantServiceFooterView
#pragma mark - 一 lazy

- (UITextField *)descTF {
    if (!_descTF) {
        _descTF = [[UITextField alloc] initWithFrame:CGRectMake(15, 0, KWidth, 30)];
        _descTF.placeholder = @"描述";
    }
    return _descTF;
}

- (UICollectionView *)clv {
    if (!_clv) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        //用来调整cell距离边框的距离。有点像CSS的padding、
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
        
        //要适配5s 、8plus h:280左右
        _clv = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 30, KWidth, 280) collectionViewLayout:layout];
        _clv.delegate = self;
        _clv.dataSource = self;
        
        _clv.backgroundColor = [UIColor whiteColor];
        
       NSString *clsName = NSStringFromClass([XJIWantServiceFooterItem class]);
        
        [_clv registerNib:[UINib nibWithNibName:clsName bundle:nil] forCellWithReuseIdentifier:clsName];
    }
    return _clv;
}


#pragma mark - 二 init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}
- (void)setupView {
    [self addSubview:self.descTF];
    [self addSubview:self.clv];
    
//    self.xjIwsImgArr = @[@1,@2,@3,@4,@5,@6,@7].mutableCopy;
//    self.xjIwsImgArr = @[@1,@2,@3,@4].mutableCopy;
//    self.xjIwsImgArr = @[@1,@2,@3].mutableCopy;

}

#pragma mark - 三 UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    // 如果 如果超过6个的话只显示6个
    if (self.xjIwsImgArr.count >= 6) {
       return 6;
    }else {
        return self.xjIwsImgArr.count + 1;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *clsName = NSStringFromClass([XJIWantServiceFooterItem class]);
    XJIWantServiceFooterItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:clsName forIndexPath:indexPath];
    
    if (indexPath.item == self.xjIwsImgArr.count) {
        item.imgIv.image = [UIImage imageNamed:@"plus"];
        item.cntLb.hidden = NO;
        item.cntLb.text = [NSString stringWithFormat:@"%ld/6",self.xjIwsImgArr.count];

        if (_xjIwsImgArr.count == 0) { //第一次要特殊处理
            item.cntLb.hidden = YES;
            item.cntLb.text = @"";
        }

        item.chaHaoButton.hidden = YES ;
    }else {
        item.imgIv.image = self.xjIwsImgArr[indexPath.item];
        item.chaHaoButton.hidden = NO;
        item.cntLb.hidden = YES;
    }
    XJLog(@"%ld",self.xjIwsImgArr.count);
    

    item.xjiwsfooterItemBlock = ^{
        XJLog(@"点击了查号:%ld",(long)indexPath.item);
        if (self.xjiwsfooterClickdeleteImgBlock) {
            self.xjiwsfooterClickdeleteImgBlock(indexPath);
        }
    };
    
    return item;
}

#pragma mark - 三 UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    //如果图片 < 6 最后一个元素可点。
    if (self.xjIwsImgArr.count < 6) { //最后一个元素可点击

        if (self.xjIwsImgArr.count == indexPath.row) {
            XJLog(@"=====>:%ld",(long)self.xjIwsImgArr.count);
            XJLog(@"+++++>:%ld",(long)indexPath.row);

            if (self.xjiwsfooterClickLastItemToAddImgBlock) {
                self.xjiwsfooterClickLastItemToAddImgBlock();
            }
        }
    }
    //超过6个就不可点击。
    
}


#pragma mark - 三 UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //左边 右边间距 15  等宽、一行3个
    CGFloat k15 = 15.0;
    CGFloat kw = (KWidth - k15*3) / 3;
    
    return CGSizeMake(kw, kw);
}

/**调整的是上下之间的距离*/
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section; {
    return 10;
}

/**interitem 左右之间的距离*/
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}


#pragma mark - 四 setter 进行赋值
//重写setter进行赋值
- (void)setXjIwsImgArr:(NSMutableArray *)xjIwsImgArr {
    _xjIwsImgArr = xjIwsImgArr;
    
    //刷新表格
    [self.clv reloadData];
}





@end
